package com.welsy.stats.command
{
	import com.welsy.stats.event.GetAllIterClassroomEvent;
	import com.welsy.stats.business.GetAllIterClassroomDelegate;
	import com.welsy.stats.model.StatsModelLocator;
	import com.welsy.stats.vo.IterClassVO;
	import com.adobe.cairngorm.commands.ICommand;
	import mx.rpc.IResponder;
	import com.adobe.cairngorm.control.CairngormEvent;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import com.welsy.util.NamespaceLocator;
	import com.welsy.qnames.ClassroomDataTypes;
	
	public class GetAllIterClassroomCommand implements ICommand, IResponder
	{
		[Bindable] private var smodel : StatsModelLocator = StatsModelLocator.getInstance();	
		
		public function execute(event : CairngormEvent):void {
			var delegate : GetAllIterClassroomDelegate = new GetAllIterClassroomDelegate(this);
			var getAllIterClassroomEvent : GetAllIterClassroomEvent = GetAllIterClassroomEvent(event);
			
			delegate.getAllIterClassroom();
		}
		
		public function result(event : Object) :void {
			//parse the server result
			var base : XMLList = XMLList(event.result);

			//add the users
			var classes : XMLList = base.descendants(ClassroomDataTypes.TAG_ITERCLASSROOMCOLLECTION).children();
			var classList : ArrayCollection = new ArrayCollection;
			//classList.removeAll();
			for each(var classT : XML in classes) {
				var buffer : IterClassVO = new IterClassVO;
				buffer.classId = classT.child(ClassroomDataTypes.TAG_CLASSROOMID);
				buffer.classTitle = classT.child(ClassroomDataTypes.TAG_CLASSROOMTITLE);
				buffer.classAuthorId = classT.child(ClassroomDataTypes.TAG_CLASSROOMAUTHORID);
				buffer.iterId = classT.child(ClassroomDataTypes.TAG_ITERID);
				
				classList.addItem(buffer);
			}
			smodel.iterClasses = classList;
		}
		
		public function fault( event : Object ):void {
			Alert.show("GetAllIterClassroom.fault!");
		}
		
	}
}